package com.jfm.jfmc.core.icc.entity;

import lombok.Data;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author DYang
 * @since 2024-10-25 22:14
 *
 * 任务控制器实体
 */
@Data
public class ControlEntity{

    /**
     * 任务ID
     */
    private String metricId;
    /**
     * 任务计数器
     */
    private AtomicInteger count;
    /**
     * 任务频率(s)
     */
    private int frequency;
    /**
     * 任务开关
     */
    private boolean enable;

    public ControlEntity(String metricId, int frequency){
        this.metricId = metricId;
        this.frequency = frequency;
        this.enable = true;
        this.count = new AtomicInteger(frequency);
    }

    public ControlEntity(String metricId, int frequency, boolean enable){
        this.metricId = metricId;
        this.frequency = frequency;
        this.enable = enable;
        this.count = new AtomicInteger(frequency);
    }

    /**
     * 减少计数器
     * @param setup 步长
     * @return true: 大于零 false: 小于等于零
     */
    public boolean countDown(int setup){
        boolean result = count.addAndGet(-(setup)) <= 0;
        if (result) {
            resetCount();
        }
        return result;
    }

    /**
     * 重置计数器
     */
    private void resetCount() {
        this.count.set(frequency);
    }

}
